package com.smartwebx.ha.web.admin.module.screen.report;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaDirectoryManager;
import com.smartwebx.ha.biz.assign.HaReportAreaManager;
import com.smartwebx.ha.biz.assign.HaReportManager;
import com.smartwebx.ha.biz.assign.HaReportUserManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaDirectory;
import com.smartwebx.ha.dal.assign.bean.HaPage;
import com.smartwebx.ha.dal.assign.bean.HaReportArea;
import com.smartwebx.ha.dal.assign.bean.HaReportUser;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.dal.assign.dao.HaAreaMapper;
/**
 * 
 * 创建人：Dream~天涯 
 * 创建时间：2015-4-22 下午5:36:05
 * 邮箱：154494796@qq.com
 * 备注：
 */
public class AjaxAddOrganize {
	@Autowired
	private HaReportManager haReportManager;
	@Autowired
	private HaDirectoryManager haDirectoryManager;
	@Autowired
	private HaReportAreaManager haReportAreaManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HaAreaMapper haAreaMapper;
	@Autowired
	private HaUserManager haUserManager;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HaReportUserManager haReportUserManager;
	public void execute(@Param("data") String dateJson,@Param("userId") String userId) throws IOException{
		HaUser h = haUserManager.findById(Integer.parseInt(userId));
		String code = h.getCode();
		int areaId = h.getAreaId();
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(
				DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		HaPage haPage = mapper.readValue(dateJson,
				HaPage.class);
		haReportManager.insert(haPage);
		String dateJson2 = dateJson.toString().substring(0, dateJson.toString().length()-1)+",\"pageId\":"+haPage.getPageId()+",\"reportId\":"+haPage.getPageId()+",\"areaId\":"+areaId+",\"code\":"+code+",\"reportType\":1}";
		
		HaReportUser haReportUser = mapper.readValue(dateJson2,
				HaReportUser.class);
		haReportUserManager.insert(haReportUser);
		
		HaDirectory haDirectory = mapper.readValue(dateJson2,
				HaDirectory.class);
		haDirectoryManager.insert(haDirectory);
		
		HaReportArea haReportArea = mapper.readValue(dateJson2,
				HaReportArea.class);
		haReportAreaManager.insert(haReportArea);
	}

}